<?php

/**
 * Created on : 2012-8-1, 16:18:08
 * @author 黄永成
 * E-mail : e.carmen.hyc@gmail.com
 * Link     http://www.houdunwang.com
 */

/**
 *  卸载指定的GET参数重组URL
 * 
 * @param  $remove  需要过滤的字段  多个以数组方式传递
 * @return String     URL地址
 */
function filterUrl($remove) {
    $get = $_GET;
    unset($get['a']);
    unset($get['c']);
    unset($get['m']);
    if (is_array($remove)) {
        foreach ($remove as $v) {
            unset($get[$v]);
        }
    } else {
        unset($get[$remove]);
    }
    $url = '';
    foreach ($get as $k => $v) {
        $url .= '/' . $k . '/' . $v;
    }
    return __METH__ . $url;
}

/**
 * 把数组写入文件
 * 
 * @param type $array   写入的数组
 * @param type $path    写入的路径
 * @return BOOL     成功TRUE，失败FALSE
 */
function filePutArray($array, $path) {
    $str = "<?php \r\n";
    $str.= "if (!defined('PATH_HD'))exit;\nreturn ";
    $str.= var_export($array, true);
    $str.= "; \r\n?>";
    return file_put_contents($path, $str);
}

/**
 * 返回发布时间字符串
 * @param type $time
 */
function getPublishTime($time) {
    $now = $_SERVER['REQUEST_TIME'];
    $str = '';
    switch ($time) {
        case $now - $time < 60:
            $str = ($now - $time) . '秒前';
            break;
        case $now - $time < 3600 :
            $str = floor(($now - $time) / 60) . '分钟前';
            break;
        case $now - $time < 3600 * 8:
            $str = floor(($now - $time) / 3600) . '小时前';
            break;
        case strtotime(date('Y-m-d', $now)) < $time:
            $str = '今天';
            break;
        case strtotime(date('Y-m-d', $now) . '-1 day') < $time:
            $str = '昨天';
            break;
        default:
            $str = date('Y-m-d', $time);
    }
    return $str;
}

/**
 * 格式化保存地区名称的ID与#
 * @param type $area    地区的字符串
 * @return type
 */
function areaformat($area) {
    return str_replace('#', ' ', preg_replace('/([0-9]+\|)/i', '', $area));
}

/**
 * 替换发送邮件内容
 * @param type $content 内容
 * @param type $keyId   验证KEY的ID
 * @param type $key     验证KEY
 * @param type $uid     用户ID
 * @param type $username    用户名
 * @return type
 */
function getEmailContent($content, $keyId, $key, $uid, $username) {
    $url = __WEB__ . '/member/index/email/key/' . $key . '/keyid/' . $keyId . '/uid/' . $uid;
    $arr = array(
        '{webname}' => C('WEB_TITLE'),
        '{username}' => $username,
        '{date}' => date('Y-m-d', $_SERVER['REQUEST_TIME']),
        '{examurl}' => '<a href = "' . $url . '" target = "_blank">' . $url . '</a>',
    );
    foreach ($arr as $k => $v) {
        $content = str_replace($k, $v, $content);
    }
    return $content;
}

/**
 * 组合ZOOM插件需要的图片数组
 * @param type $picture
 * @param type $thumb_max
 * @param type $thumb_mini
 * @return boolean|string
 */
function getZoomPictureArr($thumb_mini, $thumb_max, $picture) {
    if (!is_array($picture) || !is_array($picture) || !is_array($picture)) {
        return false;
    }
    $arr = array();
    for ($i = 0; $i < count($picture); $i++) {
        $arr[$i] = array(
            __ROOT__ . '/' . $thumb_mini[$i],
            __ROOT__ . '/' . $thumb_max[$i],
            __ROOT__ . '/' . $picture[$i],
        );
    }
    return $arr;
}

?>
